#!/bin/bash

#source $(pwd)/$(echo $0 | sed 's_/*$_/')/vbs_common_functions
SCRIPTNAME=nfilepreview
scriptPath=${0%$SCRIPTNAME}
if [ -z "$scriptPath" ]
then
  #invoked from PATH
  scriptPath=$(which $0)
  scriptPath=${scriptPath%$SCRIPTNAME}
fi
source ${scriptPath}vbs_common_functions

usage()
{
cat << EOF
$0 : Grabs n files and combines them to pipe

Usage: $0 <options> <recording name>

OPTIONS: to plot:
  -x 		number of files
EOF
}

NUMPREVFILES=3

n=1
while [ $# -gt 0 ]
do
  case $1 in
    -x) 
      NUMPREVFILES=$2
      shift
      ;;
    -s)
      FILESIZE=$2
      shift
      ;;
    -*) echo  $(date --rfc-3339=seconds) "Unknown argument $1";usage;exit 1; echo $(date --rfc-3339=seconds) "Errors in parameters" | tee -a  $LOGFILE ;;
  *) eval "arg_$n=$1";n=$(($n+1));;
esac
shift
done
RECNAME=$arg_1
if [ -z "$RECNAME" ]
then
  echo "No recname specified!"
  usage
  exit 1
fi

RECCFGFILE=$(get_cfgfile_for_recname $RECNAME)
if [ "$?" -eq "$ERR" ]
then
  echo "ERROR: No cfg for $RECNAME found"
  exit 1
fi
PACKETSIZE=$(get_packetsize_from_cfgfile $RECCFGFILE)
if [ "$?" -eq "$ERR" ]
then
  echo "ERROR: No packet_size for $RECNAME found"
  exit 1
fi
OFFSET=$(get_offset_onwrite_from_cfgfile $RECCFGFILE)
#Not checking err for this

n=0
while [ "$n" -ne "$NUMPREVFILES" ]
do
  N_FILENAME=$(get_file_n_for_recname $RECNAME $n)
  if [ "$?" -ne "0" ]
  then
    echo Cant find file $n for $RECNAME
    exit 1
  fi
  eval "file_$n=$N_FILENAME"
  n=$(($n+1))
done

COUNT=$(($FILESIZE/($PACKETSIZE+$OFFSET)))

n=0
while [ "$n" -ne "$NUMPREVFILES" ]
do
  #FILENOW=$(eval '$file_${n}')
  eval FILENOW=\$"file_${n}"
  dd if=$FILENOW bs=$(($PACKETSIZE-$OFFSET)) count=$COUNT
  if [ ! $? -eq 0 ]
  then
    echo "Error in dd"
    exit 1
  fi
  n=$(($n+1))
done


exit 0
